package com.example.androidlearning2200.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidlearning2200.R;
import com.example.androidlearning2200.bean.AppBean;
import com.example.androidlearning2200.bean.MsgBean;

import java.util.List;
import java.util.Random;

public class AppAdapter extends RecyclerView.Adapter<AppAdapter.RVHolder> {

    private List<AppBean> datas;

    public AppAdapter(List<AppBean> datas) {
        this.datas = datas;
    }

    @NonNull
    @Override
    public RVHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_app_entry, parent, false);
        return new RVHolder(view);
    }

    /**
     * 给每一个列表项填充内容
     *
     * @param holder   The ViewHolder which should be updated to represent the contents of the
     *                 item at the given position in the data set.
     * @param position The position of the item within the adapter's data set.
     */
    @Override
    public void onBindViewHolder(@NonNull RVHolder holder, int position) {
        AppBean appBean = datas.get(position);
        holder.ivAppIcon.setImageResource(appBean.getResId());
        holder.tvAppName.setText(appBean.getName());
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    /**
     * 列表项包装器
     */
    public class RVHolder extends RecyclerView.ViewHolder {

        private ImageView ivAppIcon;
        private TextView tvAppName;

        public RVHolder(@NonNull View itemView) {
            super(itemView);
            ivAppIcon = itemView.findViewById(R.id.ivAppIcon);
            tvAppName = itemView.findViewById(R.id.tvAppName);
        }

    }

}
